import http from '../http'
import qs from 'qs'
import { request, requestOnline, ResponseCode, moment } from '../baseUrl'

/**
 *  @param request 请求地址 例如：http://197.82.15.15:8088/request/...
 *  @param '/testIp'代表vue-cil中config，index.js中配置的代理
 */

/**
 * @func 今日数据
 * @param {列表对象: record}
 */
const homePageDataToday = `/homePage/data/today`
export async function getHomePageDataToday() {
  try {
    const response = await http.get(`${requestOnline}${homePageDataToday}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 近期数据
 * @param {列表对象: record}
 */
const homePageDataNearly = `/homePage/data/nearly`
export async function getHomePageDataNearly({
  pageNo = 1,
  pageSize = 20,
  appKey = '0',
  startTime = moment(moment().format('YYYY-MM-DD'))
    .subtract(7, 'days')
    .valueOf(),
  endTime = moment(moment().format('YYYY-MM-DD')).valueOf(),
}) {
  try {
    const response = await http.get(
      `${requestOnline}${homePageDataNearly}?${qs.stringify({ pageNo, pageSize, appKey, startTime, endTime })}`,
    )
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}

/**
 * @func 收益波动分析
 * @param {列表对象: record}
 */
const homePageDataIncomeAnalyse = `/homePage/data/incomeAnalyse`
export async function getHomePageDataIncomeAnalyse() {
  try {
    const response = await http.get(`${requestOnline}${homePageDataIncomeAnalyse}`)
    if (ResponseCode.success !== response.data.code) {
      const message = (typeof response.data.msg === 'string' && response.data.msg) || 'error'
      throw new Error(message)
    }
    return [null, response.data.data]
  } catch (error) {
    return [(typeof error.message === 'string' && error.message) || 'error', null]
  }
}
